<template>
    <div>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" :label-position="labelPosition" label-width="120px" class="demo-ruleForm">

        <div>
            <div v-if="showForm" class="container" style="margin-top:20px">
                <h3 style="margin-bottom: 20px">新建物料</h3>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="物料类型：">
                            {{ruleForm.materialType}}
                        </el-form-item>
						<el-form-item label="产品大类：" v-if="keliShow">
							{{ruleForm.inventoryType}}
						</el-form-item>
                        <el-form-item label="物料代码：" >
                            {{ruleForm.materialCode}}
                        </el-form-item>
                        <div v-if="keliShow">
                            <el-form-item label="发药机编码：">
                                {{ruleForm.dispenserCode}}
                            </el-form-item>
                            <!-- <el-form-item label="装量对应饮片：">
                                {{ruleForm.ypQuantity}}{{ruleForm.ypUnits}}
                            </el-form-item> -->
                            <el-form-item label="装量：" prop="spec">
                                {{ruleForm.pelletWeight}}
                                
                            </el-form-item>
                            <el-form-item label="生产备案号：">
                                {{ruleForm.prodRecordCode}}
                            </el-form-item>
                        </div>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="名称：">
                            {{ruleForm.breedName}}
                        </el-form-item>
                        <div v-if="keliShow">
                            <el-form-item label="存货代码：">
                                {{ruleForm.inventoryCode}}
                            </el-form-item>
                            <el-form-item label="规格：">
                                {{ruleForm.loadingQuantity}}{{ruleForm.loadingUnits}}
                            </el-form-item>
                        </div>
						<el-form-item label="保质期：">
						    {{ruleForm.deadLine}}月
						</el-form-item>
                        <div v-if="keliShow">
                            <el-form-item label="规格对应饮片：" class="styleWidth">
                                 {{ruleForm.ypQuantity}}{{ruleForm.ypUnits}}
                            </el-form-item>
                            <el-form-item label="储存条件：">
                                {{ruleForm.storage}}
                            </el-form-item>
                        </div>
                        <div v-if="ssssshow">
                            
                            <el-form-item label="规格码：">
                                <!-- <el-input placeholder="请输入内容" v-model.trim="ruleForm.specsCode" style="width: auto"></el-input> -->
                             {{ruleForm.specsCode}}
                            </el-form-item>
                        </div>
                    </el-col>
                </el-row>
                <el-form-item class="footerFixed">
                    <el-button type="primary" @click="submitForm">确认无误</el-button>
                    <el-button @click="showForm = false">返回修改</el-button>
                </el-form-item>
            </div>
            <div v-else class="container" style="margin-top:20px">
                <h3 style="margin-bottom: 20px">新建物料</h3>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="物料类型：" prop="materialType">
                            <el-select v-model="ruleForm.materialType" @change="changeMtype" filterable placeholder="请选择" style="width: 200px;">
                                <el-option label="中药材" value="中药材"></el-option>
                                <el-option label="辅料" value="辅料"></el-option>
                                <el-option label="提取饮片" value="提取饮片"></el-option>
								<el-option label="配方颗粒(浸膏粉)" value="配方颗粒(浸膏粉)"></el-option>
                                <el-option label="配方颗粒" value="配方颗粒"></el-option>
                            </el-select>
                        </el-form-item>
						<el-form-item v-if="keliShow" label="产品大类：" prop="inventoryType">
						    <el-select v-model="ruleForm.inventoryType"  filterable placeholder="请选择" style="width: 200px;">
						        <el-option label="C50天津中药配方颗粒" value="C50天津中药配方颗粒"></el-option>
						        <el-option label="C50中药配方颗粒" value="C50中药配方颗粒"></el-option>
						        <el-option label="C20连袋中药配方颗粒" value="C20连袋中药配方颗粒"></el-option>
								<el-option label="C20中药配方颗粒" value="C20中药配方颗粒"></el-option>
								<el-option label="C51中药配方颗粒" value="C51中药配方颗粒"></el-option>
                                <el-option label="C60中药配方颗粒" value="C60中药配方颗粒"></el-option>
                                <el-option label="C70中药配方颗粒" value="C70中药配方颗粒"></el-option>
						    </el-select>
						</el-form-item>
                        <el-form-item  label="物料代码：" prop="materialCode">
                            <el-input placeholder="请输入" v-model.trim="ruleForm.materialCode" style="width: auto"></el-input>
                        </el-form-item>
                        <!-- <el-form-item label="产地：">
                            <el-input placeholder="请输入内容" v-model="ruleForm.place" style="width: auto"></el-input>
                        </el-form-item> -->
                        <div v-if="keliShow">
                           <!-- <el-form-item label="物料代码：">
                                <el-input placeholder="请输入" v-model="ruleForm.materialCode" style="width: auto"></el-input>
                            </el-form-item> -->
                            <el-form-item label="发药机编码：">
                                <el-input placeholder="请输入内容" v-model.trim="ruleForm.dispenserCode" style="width: auto"></el-input>
                            </el-form-item>
                            <!-- <el-form-item label="装量对应饮片：" class="styleWidth">
                                <el-input placeholder="请输入" v-model.trim="ruleForm.ypQuantity" style="width: auto">
                                    <template slot="append">
                                        <el-select v-model="ruleForm.ypUnits" :disabled="isDisabled" placeholder="单位" style="width:80px">
                                          
                                            <el-option label="g" value="g"></el-option>
                                        </el-select>
                                    </template>
                                </el-input>
                            </el-form-item> -->
                            <el-form-item label="规格：" prop="spec">
                                 <el-input placeholder="请输入" v-model.trim="ruleForm.loadingQuantity" style="width: auto">
                                    <template slot="append">
                                        <el-select v-model="ruleForm.ypUnits" :disabled="isDisabled" placeholder="单位" style="width:80px">
                                          
                                            <el-option label="g" value="g"></el-option>
                                        </el-select>
                                    </template>
                                </el-input>
                                <!-- <el-input placeholder="请输入" v-model="ruleForm.pelletWeight" style="width: auto">
                                    <template slot="append" style="width: 150px">
                                        <el-select v-model="ruleForm.loadingUnits" :disabled="isDisabled" placeholder="单位" style="width:80px">
                                            
                                            <el-option label="g" value="g"></el-option>
                                        </el-select>
                                    </template>
                                </el-input> -->
                                
                            </el-form-item>
                            <el-form-item label="规格对应饮片：" class="styleWidth">
                                <el-input placeholder="请输入" v-model.trim="ruleForm.ypQuantity" style="width: auto">
                                    <template slot="append">
                                        <el-select v-model="ruleForm.ypUnits" :disabled="isDisabled" placeholder="单位" style="width:80px">
                                          
                                            <el-option label="g" value="g"></el-option>
                                        </el-select>
                                    </template>
                                </el-input>
                                <!-- <el-input placeholder="请输入" v-model.trim="ruleForm.yinpianWeight" style="width: auto">
                                    <template slot="append">
                                        <el-select v-model="ruleForm.ypUnits" :disabled="isDisabled" placeholder="单位" style="width:80px">
                                           
                                            <el-option label="g" value="g"></el-option>
                                        </el-select>
                                    </template>
                                </el-input> -->
                            </el-form-item>
                            <el-form-item label="上市备案号：">
                                <el-input placeholder="请输入内容" v-model.trim="ruleForm.prodRecordCode" style="width: auto"></el-input>
                            </el-form-item>
                        </div>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="名称：" prop="breedName">
                            <el-input placeholder="请输入内容" v-model.trim="ruleForm.breedName" style="width: auto">
                                <!-- <template slot="append">
                                    <el-select v-model="ruleForm.specUnits" placeholder="单位" style="width:80px">
                                        <el-option v-for="(item,index) in units" :key="index" :label="item.name" :value="item.name"></el-option>
                                    </el-select>
                                </template> -->
                            </el-input>
                            <!-- <el-button style="margin-left:10px" @click="unitLink">单位管理</el-button> -->
                        </el-form-item>
                      
                        <div v-if="keliShow">
                            <el-form-item label="存货代码：" prop="inventoryCode">
                                <el-input placeholder="请输入内容" v-model.trim="ruleForm.inventoryCode" style="width: auto"></el-input>
                            </el-form-item>
                            <el-form-item label="装量：" class="styleWidth">
                                <el-input placeholder="请输入" v-model="ruleForm.pelletWeight" style="width: auto">
                                    <template slot="append" style="width: 150px">
                                        <el-select v-model="ruleForm.loadingUnits" :disabled="isDisabled" placeholder="单位" style="width:80px">
                                            <el-option label="g" value="g"></el-option>
                                        </el-select>
                                    </template>
                                </el-input>
                                <!-- <el-input placeholder="请输入" v-model.trim="ruleForm.loadingQuantity" style="width: auto">
                                    <template slot="append" style="width: 150px">
                                        <el-select v-model="ruleForm.loadingUnits" :disabled="isDisabled" placeholder="单位" style="width:80px">
                                           
                                            <el-option label="g" value="g"></el-option>
                                        </el-select>
                                    </template>
                                </el-input> -->
                                <!-- <el-button style="margin-left:10px" @click="unitLink">单位管理</el-button> -->
                            </el-form-item>
                        </div>
						<el-form-item label="保质期：">
						    <el-input placeholder="请输入内容" v-model.trim="ruleForm.deadLine" style="width: auto"></el-input>
							<span style="margin-left:20px;">月</span>
						</el-form-item>
                       
                        <div v-if="keliShow">
                            
                            <el-form-item label="储存条件：">
                                <el-input placeholder="请输入内容" v-model.trim="ruleForm.storage" style="width: auto"></el-input>
                             
                            </el-form-item>
                        </div>
                        <div v-if="ssssshow">
                            
                            <el-form-item label="规格码：">
                                <el-input placeholder="请输入内容" v-model.trim="ruleForm.specsCode" style="width: auto"></el-input>
                             
                            </el-form-item>
                        </div>
                    </el-col>
                </el-row>
                <el-form-item class="footerFixed">
                    <el-button type="primary" @click="submitRules('ruleForm')">提交</el-button>
                    <el-button @click="backPage">返回</el-button>
                </el-form-item>
            </div>
        </div>
        </el-form>
    </div>
</template>


<script>
import {materialBreed_save,materialBreed_findById,materialBreed_update,unit_findByList,materialBreed_isExist,materialBreed_findInventoryCodeIsExist} from "@/request/api";
import uploadImg from '../../../common/uploadImg';
    export default {
        components: {
            uploadImg
        },
        data() {
            return {
                labelPosition:'right',
                showForm: false,
                keliShow: false,
                isDisabled: true,
                wlShow: false,
                ruleForm: {
					deadLine:'',
                    breedName:'',
                    place: '',
                    materialType: '',
                    loadingQuantity: '',
                    loadingUnits: 'g',
                    // spec: '',
                    dispenserCode: '',
                    inventoryCode: '',
                    ypQuantity: '',
                    ypUnits: 'g',
                    materialCode: ''
                },
                rules: {
                    breedName: [
                        { required: true, message: '不能为空', trigger: 'blur' }
                    ],
                    materialType: [
                        { required: true, message: '不能为空', trigger: 'change' },
                    ],
                    inventoryType: [
                        { required: true, message: '不能为空', trigger: 'blur' }
                    ],
                    inventoryCode: [
                        { required: true, message: '不能为空', trigger: 'blur' }
                    ],
                    materialCode: [
                        { required: true, message: '不能为空', trigger: 'blur' }
                    ],
					
                },
                units:[
                   
                ],
                materialTypeStor: '',
                ssssshow:false,

            };
        },
        mounted(){
            this.id = this.$route.query.id
            this.ruleForm.companyId = this.$route.query.sid
            console.log(this.ruleForm.companyId)
            if(this.id == undefined){
                if(this.$route.query.type == '中药材'){
                    this.materialTypeStor = '中药材'
                } else if(this.$route.query.type == '辅料'){
                    this.materialTypeStor = '辅料'
                } else if(this.$route.query.type == '提取饮片'){
                    this.materialTypeStor = '提取饮片'
                } else if(this.$route.query.type == '配方颗粒'){
                    this.materialTypeStor = '配方颗粒'
                }else if(this.$route.query.type == '配方颗粒(浸膏粉)'){
                    this.materialTypeStor = '配方颗粒(浸膏粉)'
                }
                this.ruleForm.materialType = this.$route.query.type
                if (this.ruleForm.materialType == '配方颗粒') {
                        this.keliShow = true
                        this.wlShow = false
                    }
                    // else if(res.data.materialType == '提取饮片'){
                    //     this.wlShow = true
                    //     this.keliShow = false
                    // }
                    else{
                        this.keliShow = false
                        this.wlShow = false
                    }
                this.$store.commit('removeRuform','')
            }else{
                this.init()
            }

             if(this.$route.query.type == '辅料' || this.$route.query.type == '配方颗粒'){
                this.ssssshow = false
            }else{
                this.ssssshow = true
            }
            // var ruleForm = this.$store.state.newStorge
            // if (ruleForm.materialType == '中药材') {
            //     this.ruleForm = this.$store.state.newStorge
            // } else if (ruleForm.inType == '辅料'){
            //     this.ruleForm = this.$store.state.newStorge
            // } else if (ruleForm.inType == '提取饮片'){
            //     this.ruleForm = this.$store.state.newStorge
            // } else if (ruleForm.inType == '配方颗粒'){
            //     this.ruleForm = this.$store.state.newStorge
            // } else{

            // }
            this.getunits()
        },
        methods: {
            getunits(){
                unit_findByList("").then((res)=>{
                    this.units = res.data
                })
            },
            backPage () {
                this.$router.go(-1);
                this.ruleForm = {}
                this.$store.commit('setStorName',this.materialTypeStor)
            },
            changeMtype(ele){
                if (ele == '配方颗粒') {
                    this.keliShow = true
                    this.wlShow = false
					this.materialCode = ""
                }
                // else if(ele == '提取饮片'){
                //     this.wlShow = true
                //     this.keliShow = false
                // }
                else{
                    this.keliShow = false
                    this.wlShow = false
                }
            },
            unitLink(){
                this.$router.push({
                    path:'/unit'
                })
                this.$store.commit('setStorge',this.ruleForm)
            },
            // 提交预览
            submitRules(formName){
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        if (this.ruleForm.materialType == '配方颗粒') {
                            // if (this.ruleForm.loadingQuantity != '') {
                            //     if(this.ruleForm.loadingUnits == ''){
                            //         this.$notify.error({
                            //             title: '提示',
                            //             message: '装量单位必填！',
                            //             showClose: false,
                            //             duration:2000
                            //         });
                            //         return false;
                            //     }
                            // }
                            // if(this.ruleForm.inventoryCode != '') {
                            
                                if (this.inventoryType == 'C50天津中药配方颗粒' || this.inventoryType == 'C50中药配方颗粒') {
                                    if (this.ruleForm.dispenserCode == '') {
                                        this.$notify.error({
                                            title: '提示',
                                            message: '发药机编码必填！',
                                            showClose: false,
                                            duration:2000
                                        });
                                        return false;
                                    }
                                }
								if(this.ruleForm.inventoryCode != '' && this.ruleForm.inventoryCode != this.inventoryCode){
									materialBreed_findInventoryCodeIsExist(this.ruleForm.inventoryCode).then((res)=>{
										if (res.data == false) {
											this.$notify.error({
												title: '提示',
												message: '存货代码重复，请重新输入！',
												showClose: false,
												duration:2000
											});
											this.ruleForm.inventoryCode = "";
											return false;
											this.showForm = false
										}else{
											this.showForm = true
										}
									})
								}
								if(this.ruleForm.inventoryCode == this.inventoryCode &&this.ruleForm.inventoryCode != ''){
									this.showForm = true
								}
                            // }
                            // if (this.ruleForm.ypQuantity != '') {
                            //     this.showForm = false
                            //     if(this.ruleForm.ypUnits == ''){
                            //         this.$notify.error({
                            //             title: '提示',
                            //             message: '装量对应饮片单位必填！',
                            //             showClose: false,
                            //             duration:2000
                            //         });
                            //         return false;
                            //     }
                            // }
                        // }
						}
						if (this.ruleForm.materialCode != "" && this.ruleForm.materialCode != this.materialCode) {
							materialBreed_isExist(this.ruleForm.materialCode).then((res) =>{
								if (res.data == false) {
									this.$notify.error({
										title: '提示',
										message: '物料代码重复，请重新输入！',
										showClose: false,
										duration:2000
									});
									this.ruleForm.materialCode = "";
									return false;
									this.showForm = false
								}else{
									this.showForm = true
								}
							})
							// return false
							// this.showForm = true
						}
						if(this.ruleForm.materialCode == this.materialCode &&this.ruleForm.materialCode != ''){
							this.showForm = true
						}
						
                    } else{
                        return false;
                    }
                })
            },
            // 添加物料事件
            submitForm() {
                // if(this.id == undefined){
                    materialBreed_save(this.ruleForm).then((res)=>{
                        this.$notify({
                            title: '提示',
                            message: '保存成功',
                            type: 'success',
                            duration:1000
                        });
                        this.backPage()
                    })
                // }else{
                //     materialBreed_update(this.ruleForm).then((res)=>{
                //         this.$notify({
                //             title: '提示',
                //             message: '修改成功',
                //             type: 'success',
                //             duration:1000
                //         });
                //         this.backPage()
                //     })
                // }
            },
            //  编辑状态
            init(){
                // 回显表单
                materialBreed_findById(this.id).then((res) => {
                    this.ruleForm = res.data;
					this.materialCode = this.ruleForm.materialCode
					this.inventoryCode = this.ruleForm.inventoryCode
                    if (res.data.materialType == '配方颗粒') {
                        this.keliShow = true
                        this.wlShow = false
                    }
                    // else if(res.data.materialType == '提取饮片'){
                    //     this.wlShow = true
                    //     this.keliShow = false
                    // }
                    else{
                        this.keliShow = false
                        this.wlShow = false
                    }
                    this.materialTypeStor = this.ruleForm.materialType
                })
            },
        }
    }
</script>
<style lang="less" scoped>
.styleWidth .el-input-group>.el-input__inner{ width: 200px !important;}
.styleWidth .el-select{ width: 60px !important;}
// .delete_btn{
//     margin-left:6px;
// }
</style>